package com.castle.fortress.admin.study.course.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.study.course.entity.StudyCourseLogEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseLogDto;
import com.castle.fortress.admin.study.course.mapper.StudyCourseLogMapper;
import com.castle.fortress.admin.study.course.service.StudyCourseLogService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 课程记录表 服务实现类
 *
 * @author castle
 * @since 2023-02-14
 */
@Service
public class StudyCourseLogServiceImpl extends ServiceImpl<StudyCourseLogMapper, StudyCourseLogEntity> implements StudyCourseLogService {
    /**
    * 获取查询条件
    * @param studyCourseLogDto
    * @return
    */
    public QueryWrapper<StudyCourseLogEntity> getWrapper(StudyCourseLogDto studyCourseLogDto){
        QueryWrapper<StudyCourseLogEntity> wrapper= new QueryWrapper();
        if(studyCourseLogDto != null){
            StudyCourseLogEntity studyCourseLogEntity = ConvertUtil.transformObj(studyCourseLogDto,StudyCourseLogEntity.class);
            wrapper.like(studyCourseLogEntity.getId() != null,"id",studyCourseLogEntity.getId());
            wrapper.like(studyCourseLogEntity.getCourseId() != null,"course_id",studyCourseLogEntity.getCourseId());
            wrapper.like(studyCourseLogEntity.getChapterId() != null,"chapter_id",studyCourseLogEntity.getChapterId());
            wrapper.like(studyCourseLogEntity.getCoursewareId() != null,"courseware_id",studyCourseLogEntity.getCoursewareId());
            wrapper.like(studyCourseLogEntity.getMemberId()!=null,"member_id",studyCourseLogEntity.getMemberId());
            wrapper.like(studyCourseLogEntity.getStatus() != null,"status",studyCourseLogEntity.getStatus());
            wrapper.like(studyCourseLogEntity.getStudyTime() != null,"study_time",studyCourseLogEntity.getStudyTime());
            wrapper.like(studyCourseLogEntity.getCreateUser() != null,"create_user",studyCourseLogEntity.getCreateUser());
            wrapper.like(studyCourseLogEntity.getCreateTime() != null,"create_time",studyCourseLogEntity.getCreateTime());
        }
        return wrapper;
    }


    @Override
    public IPage<StudyCourseLogDto> pageStudyCourseLog(Page<StudyCourseLogDto> page, StudyCourseLogDto studyCourseLogDto) {
        QueryWrapper<StudyCourseLogEntity> wrapper = getWrapper(studyCourseLogDto);
        Page<StudyCourseLogEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyCourseLogEntity> studyCourseLogPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyCourseLogDto> pageDto = new Page(studyCourseLogPage.getCurrent(), studyCourseLogPage.getSize(),studyCourseLogPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyCourseLogPage.getRecords(),StudyCourseLogDto.class));
        return pageDto;
    }


    @Override
    public List<StudyCourseLogDto> listStudyCourseLog(StudyCourseLogDto studyCourseLogDto){
        QueryWrapper<StudyCourseLogEntity> wrapper = getWrapper(studyCourseLogDto);
        List<StudyCourseLogEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,StudyCourseLogDto.class);
    }

    @Override
    public boolean updateStudyCourseLog(StudyCourseLogEntity studyCourseLogEntity) {
        return baseMapper.updateStudyCourseLog(studyCourseLogEntity);
    }

    @Override
    public void removeByCourseId(Long courseId) {
        baseMapper.removeByCourseId(courseId);
    }
}

